import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.GridLayout;
import java.awt.List;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.util.ArrayList;

import javax.imageio.ImageIO;
import javax.swing.BorderFactory;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JPanel;


public class Grid extends JPanel{
    /**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	static int gridSize =17; // MUSI BYT SUDE CISLO (kvuli koncecnym pozicim
	Color mycolor = Color.decode("#CC6600"); //nastaveni barvy policek
    ArrayList <JPanel> positions = new ArrayList<JPanel>(); 
    ArrayList <JPanel> walkBoxes = new ArrayList<JPanel>(); 
    ArrayList <JPanel> startPos = new ArrayList<JPanel>();
    ArrayList <JPanel> endPos = new ArrayList<JPanel>();
	JPanel KingGrid;
	
	
	public Grid(){
	
		KingGrid = new JPanel(new GridLayout(gridSize,gridSize));
		
		//cyklus plni KingGrid dilcimi JPanely a barvi je na cernou
		for (int i=0; i<gridSize*gridSize; i++) {
			positions.add(new JPanel(new BorderLayout()));
			
			KingGrid.add(positions.get(i));
			positions.get(i).setOpaque(false);
		}
		
	
	Incrementor inc = new Incrementor();
	inc.setincrementor(2*gridSize+2);
	
	// soubor funkci k tvoreni hlavni cesty pro figurky - dynamicky se meni podle velikosti hriste
	
	// horni cast cesty 
	for (int i = 0; i < gridSize - 4; i++){
		int index = inc.getincrementor();
		walkBoxes.add(positions.get(index));
		index += 1;
		inc.setincrementor(index);
	}
	// prava cast cesty
	for (int i = 0; i < gridSize - 5; i++){
		int index = inc.getincrementor()+gridSize;
		walkBoxes.add(positions.get(index-1));
		
		if (i == gridSize - 5){
		inc.setincrementor(index + gridSize);	
		}
		else{
		inc.setincrementor(index);
		}	
	}
	
	//spodni cast cesty
	for (int i = 0; i < gridSize - 4; i++){
		int index = inc.getincrementor()-1;
		walkBoxes.add(positions.get(index));
		inc.setincrementor(index);
	}
	
	// leva cast cesty
	for (int i = 0; i < gridSize - 5; i++){
		int index = inc.getincrementor()-gridSize;
		walkBoxes.add(positions.get(index));
		inc.setincrementor(index);
	}
	

	for (int i = 0; i < walkBoxes.size(); i++){
			walkBoxes.get(i).setBackground(mycolor);
			walkBoxes.get(i).setOpaque(true);
			walkBoxes.get(i).setBorder(BorderFactory.createLineBorder(Color.black));
	}
	
		
	//tvoreni zacatecnich pozic
	for (int i=0;i<4;i++){
		if (i==0){
			startPos.add(positions.get(0));
			startPos.add(positions.get(gridSize));
			startPos.add(positions.get(1));
			startPos.add(positions.get(1+gridSize));
		}
		if (i==1){
			startPos.add(positions.get(0+gridSize-2));
			startPos.add(positions.get(0+2*gridSize-2));
			startPos.add(positions.get(0+gridSize-1));
			startPos.add(positions.get(0+2*gridSize-1));
		}
		if (i==2){
			startPos.add(positions.get((gridSize-2)*gridSize));
			startPos.add(positions.get((gridSize-2)*gridSize+gridSize));
			startPos.add(positions.get((gridSize-2)*gridSize+1));
			startPos.add(positions.get((gridSize-2)*gridSize+1+gridSize));
		}
		if(i==3){
			int start = ((gridSize-2)*gridSize);
			startPos.add(positions.get(start+gridSize-2));
			startPos.add(positions.get(start+2*gridSize-2));
			startPos.add(positions.get(start+gridSize-1));
			startPos.add(positions.get(start+2*gridSize-1));
		}
	}
	
	for (int i = 0; i < startPos.size(); i++){
		startPos.get(i).setBackground(mycolor);
		startPos.get(i).setOpaque(true);
		startPos.get(i).setBorder(BorderFactory.createLineBorder(Color.black));
		}

	//---------tvoreni konecnych policek
	
	//konec prvniho hrace
	int endposone = 3*gridSize+(int)Math.ceil((gridSize-6)/2)+3;
	for (int i=0;i<4;i++){
	endPos.add(positions.get(endposone+i*gridSize));	
	}
	
	// konec druheho hrace
	int endpostwo = (((int)Math.ceil(gridSize-6)/2)+3)*gridSize+gridSize-4;
	for (int i=0;i<4;i++){
	endPos.add(positions.get(endpostwo-i));
	}
	//konec tretiho hrace
	int endposthree=(gridSize-4)*gridSize+(int)Math.ceil((gridSize-6)/2)+3;
	for (int i=0;i<4;i++){
		endPos.add(positions.get(endposthree-i*gridSize));
	}
	//konec ctvrteho hrace
	int endposfour = (((int)Math.ceil((gridSize-6)/2+3))*gridSize+3);
	for (int i=0;i<4;i++){
	endPos.add(positions.get(endposfour+i)); 
	}
	
	for (int i = 0; i < endPos.size(); i++){
		
		endPos.get(i).setBackground(mycolor);
		endPos.get(i).setOpaque(true);
		endPos.get(i).setBorder(BorderFactory.createLineBorder(Color.black));
		}
	walkBoxes.addAll(endPos);
}

	
	
	public JPanel getGrid(){
		return KingGrid;
	}
	
}
